package com.qianxin.ids.idam.am.authn.factors.cass.upsso;

import com.gitee.starblues.realize.BasePlugin;
import com.gitee.starblues.realize.OneselfListener;
import com.gitee.starblues.realize.PluginUtils;
import com.gitee.starblues.utils.OrderPriority;
import com.qianxin.ids.idam.am.authn.factors.cass.upsso.refreshToken.RefreshTokenTask;
import com.qianxin.ids.idam.am.common.api.entity.AuthnLogBusinessTypeEnum;
import com.qianxin.ids.idam.bizcomps.common.iface.cluster.IClusterService;
import com.qianxin.ids.idam.framework.common.utils.ServerModeHelper;
import com.qianxin.ids.idam.framework.timer.common.TimerException;
import com.qianxin.ids.idam.framework.timer.iface.ITimerService;
import com.qianxin.ids.idam.sdk.context.SpringBeanFactory;
import com.qianxin.ids.idam.sdk.utils.DynamicEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class PluginCassUpSsoAuthListener implements OneselfListener {
    @Autowired
    private PluginCassUpSsoAuthnFactorImpl pluginCassUpSsoAuthnFactor;
    @Autowired
    private RefreshTokenTask task;

    @Autowired
    private PluginUtils pluginUtils;
    private ITimerService timer;
    private ServerModeHelper serverModeHelper;
    private IClusterService clusterService;

    @Override
    public OrderPriority order() {
        return OrderPriority.getMiddlePriority();
    }

    @PostConstruct
    private void init() {
        timer = pluginUtils.getMainBean(ITimerService.class);
        serverModeHelper = pluginUtils.getMainBean(ServerModeHelper.class);
        clusterService = SpringBeanFactory.getBean("clusterService");
    }

    @Override
    public void startEvent(BasePlugin basePlugin) {
        log.info("install plugin cass up sso  !!!");
        pluginCassUpSsoAuthnFactor.hotReload();
        DynamicEnumUtil.addEnum(AuthnLogBusinessTypeEnum.class, "CASS_UP_SSO", new Class<?>[]{String.class, String.class},
                new Object[]{"中国社会科学院UP认证", "cass_up_sso"});
        log.info("开始尝试初始化任务相关需求信息");

        // admin侧还是需要初始化redis缓存。
        task.reload();
        // 只在runtime测 主节点 初始化定时任务。
        // 添加定时任务
        try {
            log.info("RefreshTokenTask ==> 开始添加定时任务");
            timer.addTask(task
                    , System.currentTimeMillis()
                    , -1
                    , task.getTime()
                    , task.getTaskName()
            );
            log.info("RefreshTokenTask ==> 定时任务添加成功");
        } catch (TimerException e) {
            log.info("spa timer task is already exist, ignore add it.");
            e.printStackTrace();
        }

        log.info("install plugin cass up sso  over !!!");

    }

    @Override
    public void stopEvent(BasePlugin basePlugin) {
        log.info("uninstall plugin auth cass up sso  !!!");
        pluginCassUpSsoAuthnFactor.hotUnload();

        try {
            task.clear();
            timer.removeTask(new RefreshTokenTask().getTaskName());
        } catch (TimerException e) {
            log.info("remove task fail.");
            e.printStackTrace();
        }
    }


}
